package net.homeip.jcperera.nu.core.utils;

import java.io.Serializable;

import net.homeip.jcperera.nu.core.CoreBase;

/**
 * Provides utility functions related to string manipulation
 * 
 * @name Strig Manipulations Utility class
 * @author julio.c.perera@gmail.com
 * @application nu
 * @since 0.1.0.0@
 */

public class StringUtil extends CoreBase implements Serializable
{
    // Required to implement Serializable interface
    private static final long  serialVersionUID = -1404202393956019966L;

    // CoreBase properties, for easy use from other components, as well as logging
    public static final int    runtimeLevel     = 0;
    public static final String moduleName       = "core";
    public static final String componentName    = "nu_core_utils_string_util";

    /**
     * Returns the <code>pos</code> first characters of the given {@link String}, if the specified {@link String} is
     * <code>null</code> or <code>pos</code> is no greater than zero, then an empty string is returned
     * 
     * @param str
     *            the {@link String} to return the substring
     * @param pos
     *            the maximum number of characters to return
     * @return the substring
     * @see String#substring(int, int)
     */
    public static String left(String str, int pos)
    {
        if ((str == null) || (pos <= 0)) return "";
        else return str.substring(0, pos);
    }

    /**
     * Returns the <code>pos</code> last characters of the given {@link String}, if the specified {@link String} is
     * <code>null</code> or <code>pos</code> is no greater than zero, then an empty string is returned
     * 
     * @param str
     *            the {@link String} to return the substring
     * @param pos
     *            the maximum number of characters to return
     * @return the substring
     * @see String#substring(int, int)
     */
    public static String right(String str, int pos)
    {
        if ((str == null) || (pos <= 0)) return "";
        int strLen = str.length();
        return str.substring(Math.max(0, strLen - pos), strLen);
    }

    /**
     * Pads the {@link String} with spaces to the right to the given length as specified in {@code count}
     * 
     * @param str
     *            The {@link String} to pad
     * @param count
     *            The length of the resulting {@link String}
     * @return The padded {@link String}
     */
    public static String padRightWithSpace(String str, int count)
    {
        return String.format("%1$-" + count + "s", str);
    }

    /**
     * Pads the {@link String} with spaces to the left to the given length as specified in {@code count}
     * 
     * @param str
     *            The {@link String} to pad
     * @param count
     *            The length of the resulting {@link String}
     * @return The padded {@link String}
     */
    public static String padLeftWithSpace(String str, int count)
    {
        return String.format("%1$" + count + "s", str);
    }

    /**
     * Pads the {@link String} with the specified {@code char} to the left to the given length as specified in
     * {@code count}
     * 
     * @param str
     *            The {@link String} to pad
     * @param count
     *            The length of the resulting {@link String}
     * @param pad
     *            The character to use when padding
     * @return The padded {@link String}
     */
    public static String padLeft(String str, int count, char pad)
    {
        return padLeftWithSpace(str, count).replace(' ', pad);
    }
    
    /**
     * Pads the {@link String} with the specified {@code char} to the right to the given length as specified in
     * {@code count}
     * 
     * @param str
     *            The {@link String} to pad
     * @param count
     *            The length of the resulting {@link String}
     * @param pad
     *            The character to use when padding
     * @return The padded {@link String}
     */
    public static String padRight(String str, int count, char pad)
    {
        return padRightWithSpace(str, count).replace(' ', pad);
    }
    
}
